﻿using System;
using System.Collections.Generic;
using System.Text;

namespace test.compiler
{
    class helper
    {
        public static TT.Compiler.Library CompileCode(string codefile)
        {


            var path = System.IO.Path.GetDirectoryName(typeof(helper).Assembly.Location);
            var cc = new TT.Compiler.CompilerContext(new TT.Compiler.ConsoleLogger(), true);
            var srccode = System.IO.File.ReadAllText(System.IO.Path.Combine(path, codefile));

            //var c1 = srccode[0];
            //var c2 = srccode[1];
            //Console.WriteLine("===========test ast");
            //var parser = dumptool.GetParserInfo<ttparser.ttlangLexer, ttparser.ttlangParser>("root");

            //var rc = parser.Parse(srccode, false, false, true);

            //Console.WriteLine("===========test code");

            var lib = cc.CreateLib_Simple(srccode);
            return lib;
        }
        public static void DumpLibDefine(TT.Compiler.Library lib)
        {
            foreach (var def in lib.typeDef)
            {
                string space = "";
                DumpDefine_Type(def.Value, space);
            }
        }
        static void DumpDefine_Type(TT.Compiler.Define.TypeDefine type, string space)
        {
            Console.WriteLine(space + "==type begin==" + type.FullName + ":" + type.Type);
            var mspace = space + "    ";
            foreach (var f in type.Fields)
            {
                Console.WriteLine(mspace + f.Name + " = " + f.DefaultValue);
            }
            foreach (var f in type.Funcs)
            {
                Console.WriteLine(mspace + f.Name + "()");
            }
            Console.WriteLine(space + "==type end==" + type.FullName + ":" + type.Type);
        }

        static Antlr4.Runtime.RuleContext ParseAst(string text)
        {
            Antlr4.Runtime.AntlrInputStream input = new Antlr4.Runtime.AntlrInputStream(text);
            var lex = new ttparser.math4Lexer(input);
            var tokenstream = new Antlr4.Runtime.CommonTokenStream(lex);
            var parser = new ttparser.ttlangParser(tokenstream);
            var exprroot = parser.root();
            return exprroot;
        }
    }
}
